//
// Created by 33145 on 2023/6/8.
//

#include "rpc_controller.h"

namespace rocket {
  void RPCController::Reset() {
    _error_code = 0;
    _error_info = "";
    _msg_id = "";
    _is_failed = false;
    _is_cancel = false;
    _is_finished = false;
    _local_addr = nullptr;
    _peer_addr = nullptr;
    _timeout = 1000;   // ms
  }

  bool RPCController::Failed() const {
    return _is_failed;
  }

  std::string RPCController::ErrorText() const {
    return _error_info;
  }

  void RPCController::StartCancel() {
    _is_cancel = true;
    _is_failed = true;
    SetFinished(true);
  }

  void RPCController::SetFailed(const std::string &reason) {
    _is_failed = true;
    _error_info = reason;
  }

  bool RPCController::IsCanceled() const {
    return _is_cancel;
  }

  void RPCController::NotifyOnCancel(google::protobuf::Closure *callback) {

  }

  void RPCController::SetError(int32_t error_code, const std::string& error_info) {
    _error_code = error_code;
    _error_info = error_info;
    _is_failed = true;
    SetFinished(true);
  }

  int32_t RPCController::GetErrorCode() const {
    return _error_code;
  }

  std::string RPCController::GetErrorInfo() {
    return _error_info;
  }

  void RPCController::SetMsgId(const std::string& msg_id) {
    _msg_id = msg_id;
  }

  std::string RPCController::GetMsgId() {
    return _msg_id;
  }

  void RPCController::SetLocalAddr(NetAddr::ptr addr) {
    _local_addr = addr;
  }

  void RPCController::SetPeerAddr(NetAddr::ptr addr) {
    _peer_addr = addr;
  }

  NetAddr::ptr RPCController::GetLocalAddr() {
    return _local_addr;
  }

  NetAddr::ptr RPCController::GetPeerAddr() {
    return _peer_addr;
  }

  void RPCController::SetTimeout(int timeout) {
    _timeout = timeout;
  }

  int RPCController::GetTimeout() {
    return _timeout;
  }

  bool RPCController::Finished() {
    return _is_finished;
  }

  void RPCController::SetFinished(bool value) {
    _is_finished = value;
  }
}